﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using Ionic.Zip;



namespace GieldaCSharp
{
    class Aktualizuj
    {
        private DateTime dateTime = new DateTime();
        private string _folder = Application.StartupPath.ToString() + "\\notowania\\";
        private string _path = "http://bossa.pl/pub/ciagle/omega/cgl/";

        public Aktualizuj()
        {
            
        }

        public void Start()
        {
            dateTime = DateTime.Today;
            int miesiac = dateTime.Month;
            int rok = dateTime.Year;

            if (!Directory.Exists(Application.StartupPath.ToString() + "\\notowania"))
            {
                Directory.CreateDirectory(Application.StartupPath.ToString() + "\\notowania");
            }

            Console.WriteLine("Trwa aktualizacja...");            

            //-------------------------------Aktualizacja dni--------------------------------
            while (dateTime.Year > 1999)
            {
                if (ZnajdzDzien(dateTime)) return;

                AktualizujDzien(dateTime);

                if (dateTime.Month < miesiac)
                {
                    miesiac = dateTime.Month;

                    if (AktualizujMiesiac(dateTime))
                    {
                        dateTime = dateTime.AddMonths(-1);
                        int m = dateTime.Month;
                        while (dateTime.Month == m)
                        {
                            dateTime = dateTime.AddDays(1);
                        }
                        dateTime = dateTime.AddDays(-1);
                        if (dateTime.DayOfWeek == DayOfWeek.Saturday) dateTime = dateTime.AddDays(-1);
                        if (dateTime.DayOfWeek == DayOfWeek.Sunday) dateTime = dateTime.AddDays(-2);
                    }
                }

                if (dateTime.Year < rok)
                {
                    rok = dateTime.Year;
                    if (AktualizujRok(dateTime))
                    {
                        dateTime = dateTime.AddYears(-1);

                        int y = dateTime.Year;
                        while (dateTime.Year == y)
                        {
                            dateTime = dateTime.AddMonths(1);
                        }
                        dateTime = dateTime.AddMonths(-1);


                        int m = dateTime.Month;
                        while (dateTime.Month == m)
                        {
                            dateTime = dateTime.AddDays(1);
                        }
                        dateTime = dateTime.AddDays(-1);
                    }
                }

                dateTime = dateTime.AddDays(-1);
            }                       
        }

        private void AktualizujDzien(DateTime d)
        {
            Data data = new Data();
            string name = data.wezDateInt(d).ToString() + ".txt";

            DownloadFile(_path + name, _folder + name);
        }

        private bool AktualizujMiesiac(DateTime d)
        {
            Data data = new Data();
            string name = "";
            if (d.Month < 10) name += "0" + d.Month;
            else name += d.Month;
            name += "-" + d.Year + ".zip";

            if (DownloadFile(_path + name, _folder + name))
            {
                UnzipMonth(name);
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool AktualizujRok(DateTime d)
        {
            string name = d.Year + ".zip";

            if (DownloadFile(_path + name, _folder + name))
            {
                UnzipYear(name);
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool ZnajdzDzien(DateTime d)
        {
            Data data = new Data();
            return File.Exists(_folder + data.wezDateInt(d).ToString() + ".txt");
        }

        private bool DownloadFile(string path, string f_name)
        {
            WebClient wb = new WebClient();

            try
            {
                wb.Dispose();
                if (!wb.IsBusy)
                {
                    try
                    {
                        wb.DownloadFile(path, f_name);
                    }
                    catch (WebException e)
                    {
                        Console.WriteLine(e);
                        Console.WriteLine("Nie mozna znalezc strony: " + path);
                        return false;
                    }
                }
                else
                {
                    Console.WriteLine("IsBusy! " + path);
                    return false;
                }
            }
            catch
            {
                Console.WriteLine("Brak strony! " + path);
                return false;
            }
            wb.Dispose();
            return true;
        }

        private void UnzipMonth(string file)
        {
            ZipFile zip = ZipFile.Read(_folder + file);
            zip.ExtractAll(_folder, ExtractExistingFileAction.DoNotOverwrite);
            zip.Dispose();
            File.Delete(_folder + file);
        }

        private void UnzipYear(string file)
        {
            ZipFile zip = ZipFile.Read(_folder + file);

            foreach (ZipEntry en in zip)
            {
                en.Extract();
                if (ZipFile.IsZipFile(en.FileName))
                {
                    ZipFile zip2 = ZipFile.Read(en.FileName);
                    zip2.ExtractAll(_folder, ExtractExistingFileAction.DoNotOverwrite);
                    zip2.Dispose();
                    File.Delete(Application.StartupPath.ToString() + "\\" + en.FileName);
                }
            }
            zip.Dispose();
            File.Delete(_folder + file);
        }
    }
}
